package com.example.demo_yunpan.service.impl;

import com.example.demo_yunpan.entity.user;
import com.example.demo_yunpan.mapper.UserMapper;
import com.example.demo_yunpan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    //@Autowired
    //private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<user> getUserByAll() {
        return userMapper.getUserByAll();
    }

    @Override
    public user userLogin(String userName, String userPass) {
        return userMapper.userLogin(userName,userPass);
    }


    @Override
    public int userRegister(String userName,String userPass) {
        user u = new user();
        u.setUserName(userName);
        u.setUserPass(userPass);

        // 获取当前创建时间（LocalDateTime）
        LocalDateTime now = LocalDateTime.now();
        // 将LocalDateTime转换为Instant（时区敏感）
        java.time.Instant instant = now.atZone(ZoneId.systemDefault()).toInstant();
        // 将Instant转换为Timestamp
        Timestamp timestamp = Timestamp.from(instant);
        u.setRegisterTime(timestamp);
        //判断是否存在重复的用户名
        user findUser = userMapper.selectUserByName(userName);
        if(findUser == null){
            return userMapper.insertUser(u);
        }else{
            return 0;
        }
    }

    @Override
    public user selectUserById(String userId) {
        return userMapper.selectUserById(userId);
    }

    @Override
    public int updateUser(user u) {
        return userMapper.updateUser(u);
    }

    /*@Override
    public String getUserWithAvatar(String userName) {
        user user = userMapper.selectUserByName(userName);
        return user.getAvatarHdfs();
    }*/
}
